package com.titleevaluation.controller;

import com.titleevaluation.entity.AnnualAssessment;
import com.titleevaluation.service.AnnualAssessmentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/annual-assessments")
public class AnnualAssessmentController {

    private final AnnualAssessmentService annualAssessmentService;

    public AnnualAssessmentController(AnnualAssessmentService annualAssessmentService) {
        this.annualAssessmentService = annualAssessmentService;
    }

    @PostMapping
    public ResponseEntity<AnnualAssessment> createAnnualAssessment(@PathVariable Long applicationId, @RequestBody AnnualAssessment annualAssessment) {
        AnnualAssessment createdAnnualAssessment = annualAssessmentService.createAnnualAssessment(applicationId, annualAssessment);
        return ResponseEntity.ok(createdAnnualAssessment);
    }

    @GetMapping
    public ResponseEntity<List<AnnualAssessment>> getAnnualAssessmentsByApplicationId(@PathVariable Long applicationId) {
        List<AnnualAssessment> annualAssessments = annualAssessmentService.getAnnualAssessmentsByApplicationId(applicationId);
        return ResponseEntity.ok(annualAssessments);
    }

    @GetMapping("/{id}")
    public ResponseEntity<AnnualAssessment> getAnnualAssessmentById(@PathVariable Long id) {
        return annualAssessmentService.getAnnualAssessmentById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<AnnualAssessment> updateAnnualAssessment(@PathVariable Long id, @RequestBody AnnualAssessment annualAssessment) {
        AnnualAssessment updatedAnnualAssessment = annualAssessmentService.updateAnnualAssessment(id, annualAssessment);
        return ResponseEntity.ok(updatedAnnualAssessment);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAnnualAssessment(@PathVariable Long id) {
        annualAssessmentService.deleteAnnualAssessment(id);
        return ResponseEntity.noContent().build();
    }
}

